package com.elm.controller;

import com.elm.common.ApiResponse;
import com.elm.po.Cart;
import com.elm.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api")
public class CartController {
    @Autowired
    private CartService cartService;

    @PostMapping("/carts")
    public ApiResponse<Cart> saveCart(@RequestBody Cart cart) {
    	try {
    		Cart createdCart=cartService.addCart(cart);
    		if(createdCart ==null) {
    			return ApiResponse.error("500","保存购物车失败");
    		}
    		return ApiResponse.success(createdCart);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }   
    }

    @PutMapping("/carts")
    public ApiResponse<Cart> updateCart(@RequestBody Cart cart) {
    	try {
    		Cart createdCart =cartService.updateCart(cart);
    		return ApiResponse.success(createdCart);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }   
    }

    @DeleteMapping("/carts")
    public ApiResponse<Integer> removeCart(@RequestBody Cart cart) {
    	try {
    		int result = cartService.removeCart(cart);
    		if(result==0) {
    			return ApiResponse.error("500","删除购物车失败");
    		}
    		return ApiResponse.success(result);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }   
    }

    @GetMapping("/carts/{businessId}/{userId}")
    public ApiResponse<List<Cart>> getCart(@PathVariable Integer businessId,@PathVariable Integer userId) {
    	try {
    		List<Cart> result = cartService.getCart(businessId,userId);
//    		if(result.size()==0) {
//    			return ApiResponse.error("500","获取购物车失败");
//    		}
    		return ApiResponse.success(result);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }   
    }

    @GetMapping("/add/{x}/{y}")
    public double add(@PathVariable double x,@PathVariable double y) {
        BigDecimal bd1 = new BigDecimal(Double.toString(x));
        BigDecimal bd2 = new BigDecimal(Double.toString(y));
        BigDecimal sum = bd1.add(bd2);
        return sum.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    @GetMapping("/mul/{x}/{y}")
    public double mul(@PathVariable double x,@PathVariable double y) {
        BigDecimal bd1 = new BigDecimal(Double.toString(x));
        BigDecimal bd2 = new BigDecimal(Double.toString(y));
        BigDecimal product = bd1.multiply(bd2);
        return product.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

}